import { useHttp } from '@src/utils/http';
import { QueryKey, useMutation, useQuery } from 'react-query';
import { Kanban } from '@src/types/kanban';
import { useAddConfig, useDeleteConfig, useReorderKanbanConfig } from '@src/utils/use-optimistic-options';

/**
 * =========================  查询面板  ==========================
 * @param param
 */
export const useKanbans = (param?: Partial<Kanban>) => {
  const client = useHttp();
  return useQuery<Kanban[]>(['kanbans', param], () => client('kanbans', { data: param }));
};

/**
 * =========================  添加看板  ==========================
 * @param queryKey
 */
export const useAddKanban = (queryKey: QueryKey) => {
  const client = useHttp();

  return useMutation(
    (params: Partial<Kanban>) =>
      client(`kanbans`, {
        data: params,
        method: 'POST',
      }),
    useAddConfig(queryKey)
  );
};

/**
 * =========================  删除看板  ==========================
 * @param queryKey
 */
export const useDeleteKanban = (queryKey: QueryKey) => {
  const client = useHttp();

  return useMutation(
    ({ id }: { id: number }) =>
      client(`kanbans/${id}`, {
        method: 'DELETE',
      }),
    useDeleteConfig(queryKey)
  );
};

/**
 * =========================  查询面板  ==========================
 * @param queryKey
 */
export interface SortProps {
  // 要重新排序的 item
  fromId: number;
  // 目标 item
  referenceId: number;
  // 放在目标item的前还是后
  type: 'before' | 'after';
  fromKanbanId?: number;
  toKanbanId?: number;
}
export const useReorderKanban = (queryKey: QueryKey) => {
  const client = useHttp();
  return useMutation((params: SortProps) => {
    return client('kanbans/reorder', {
      data: params,
      method: 'POST',
    });
  }, useReorderKanbanConfig(queryKey));
};
